<?php

class PetCategoryController extends CController
{
	const PAGE_SIZE=10;

	public $defaultAction='list';
	private $_petcategory;

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'users'=>array('admin'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	public function actionShow()
	{
		$this->render('show',array('petcategory'=>$this->loadPetCategory()));
	}

	public function actionCreate()
	{
		$petcategory=new PetCategory;
		if(isset($_POST['PetCategory'])) {
			$petcategory->attributes=$_POST['PetCategory'];
			$petcategory->normalImage=CUploadedFile::getInstance($petcategory,'normalImage');
			$petcategory->attackImage=CUploadedFile::getInstance($petcategory,'attackImage');
			$petcategory->defenceImage=CUploadedFile::getInstance($petcategory,'defenceImage');
			if($petcategory->save())
				$this->redirect(array('show','id'=>$petskill->id));
		}

		$this->render('create',array('petcategory'=>$petcategory));
	}

	public function actionUpdate()
	{
		$petcategory=$this->loadPetCategory();
		if(isset($_POST['PetCategory'])) {
			$petcategory->attributes=$_POST['PetCategory'];
			$petcategory->normalImage=CUploadedFile::getInstance($petcategory,'normalImage');
			$petcategory->attackImage=CUploadedFile::getInstance($petcategory,'attackImage');
			$petcategory->defenceImage=CUploadedFile::getInstance($petcategory,'defenceImage');
			if($petcategory->save())
				$this->redirect(array('show','id'=>$petskill->id));
		}

		$this->render('update',array('petcategory'=>$petcategory));
	}

	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			$this->loadPetCategory()->delete();
			$this->redirect(array('list'));
		}
		else
			throw new CHttpException(500,'Invalid request. Please do not repeat this request again.');
	}

	public function actionList()
	{
		$this->processAdminCommand();

		$criteria=new CDbCriteria;

		$pages=new CPagination(PetCategory::model()->count($criteria));
		$pages->pageSize=self::PAGE_SIZE;
		$pages->applyLimit($criteria);

		$sort=new CSort('PetCategory');
		$sort->applyOrder($criteria);

		$petcategoryList=PetCategory::model()->findAll($criteria);

		$this->render('list',array(
			'petcategoryList'=>$petcategoryList,
			'pages'=>$pages,
			'sort'=>$sort,
		));
	}

	public function loadPetCategory($id=null)
	{
		if($this->_petcategory===null)
		{
			if($id!==null || isset($_GET['id']))
				$this->_petcategory=PetCategory::findById($id!==null ? $id : $_GET['id']);
			if($this->_petcategory===null)
				throw new CHttpException(500,'The requested petcategory does not exist.');
		}
		return $this->_petcategory;
	}

    protected function processAdminCommand()
    {
        if(isset($_POST['command'], $_POST['id']) && $_POST['command']==='delete')
        {
            $this->loadPetCategory($_POST['id'])->delete();
            $this->refresh();
        }
    }
}
